package com.sunlg.smarthome.contract.presenter;

import com.sunlg.smarthome.base.App;
import com.sunlg.smarthome.base.C;
import com.sunlg.smarthome.data.entity.User;
import com.sunlg.smarthome.data.remote.MySubscriber;
import com.sunlg.smarthome.data.remote.Result;
import com.sunlg.smarthome.data.remote.SHServiceWrapper;
import com.sunlg.smarthome.contract.LoginContract;
import com.sunlg.smarthome.utils.JPushUtils;
import com.sunlg.smarthome.utils.PreferHelper;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by sunlg on 2017/3/2.
 */

public class LoginPresenter implements LoginContract.Presenter{

    private LoginContract.View mView;
    private SHServiceWrapper mSHServiceWrapper;
    private PreferHelper mPreferHelper;

    private String pwd;

    public LoginPresenter() {
        mSHServiceWrapper = App.getInstance().getServiceWrapper();
        mPreferHelper = App.getInstance().getPreferHelper();
    }

    @Override
    public void silenceLogin(String username, String password) {
        Map<String, Object> map = new HashMap<>();
        map.put("username", username);
        map.put("password", password);
        pwd = password;
        mSHServiceWrapper.login(map, new MySubscriber<Result<User>>() {
            @Override
            public void onSuccess(Result<User> data) {
                saveUserMsg(data);
            }

            @Override
            public void _onError(Throwable e) {
                mView.showError(e.getMessage());
            }
        });
    }

    @Override
    public void login(String username, final String password) {
        Map<String, Object> map = new HashMap<>();
        map.put("username", username);
        map.put("password", password);
        pwd = password;
        mView.showLoading();
        mSHServiceWrapper.login(map, new MySubscriber<Result<User>>() {
            @Override
            public void onSuccess(Result<User> data) {
                if(data.getCode() == C.ActionStatus.SUCCESS){
                    mView.hideLoading();
                    saveUserMsg(data);
                    new JPushUtils().setAlias(String.valueOf(data.getData().getId()));
                }else {
                   _onError(new Throwable(data.getMsg()));
                }

            }

            @Override
            public void _onError(Throwable e) {
                mView.hideLoading();
                mView.showError(e.getMessage());
                //mView.forward();
            }
        });
    }

    @Override
    public void attachView(LoginContract.View view) {
        this.mView = view;
    }

    @Override
    public void dettachView() {
        this.mView = null;
    }

    /**
     * 保存账号信息
     * @param data
     */
    private void saveUserMsg(Result<User> data){
        if(data.getCode() == C.ActionStatus.SUCCESS){
            //保存账号信息
            User user = data.getData();
            if(mPreferHelper.getBoolean(C.PreKey.IS_REMEMBER, false)){
                mPreferHelper.setString(C.PreKey.PASSWORD, pwd);
            }
            if(user != null) {
                mPreferHelper.setInt(C.PreKey.USER_ID, user.getId());
                mPreferHelper.setString(C.PreKey.USER_NAME, user.getUsername());
                mPreferHelper.setString(C.PreKey.EMAIL, user.getEmail());
                mPreferHelper.setString(C.PreKey.PHONE, user.getPhone());
            }
            mView.forward();
        }else {
            mView.showError(data.getMsg());
        }
    }
}
